package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.SysSheetUsers;
import com.ruoyi.system.mapper.SysSheetUsersMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysSheetUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class SysSheetUsersServiceImpl implements ISysSheetUsersService {

    @Autowired
    private SysSheetUsersMapper sysSheetUsersMapper;

    @Override
    @Transactional
    public boolean addSysSheetUsers(SysSheetUsers sysSheetUsers) {
        try {
            sysSheetUsersMapper.delete(new LambdaQueryWrapper<SysSheetUsers>()
                    .eq(SysSheetUsers::getSysUserid, sysSheetUsers.getSysUserid()));
            List<SysSheetUsers> list = new ArrayList<>();
            Arrays.stream(sysSheetUsers.getSheetids()).forEach(item ->{
                SysSheetUsers sheetUsers = new SysSheetUsers();
                sheetUsers.setSysUserid(sysSheetUsers.getSysUserid());
                sheetUsers.setSheetid(item);
                list.add(sheetUsers);
            });
            list.forEach(item ->{
                sysSheetUsersMapper.insert(item);
            });
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }
}
